//
//  MTLoginFristController.m
//  MeToo
//
//  Created by 俊松何 on 2018/7/5.
//  Copyright © 2018年 tianxie. All rights reserved.
//

#import "MTLoginFristController.h"
#import "MTLoginViewController.h"
#import "MTRegisterViewController.h"

@interface MTLoginFristController ()
@property (weak, nonatomic) IBOutlet UIImageView *bgImageView;

@property (nonatomic,strong) UIImageView *bgImageView1;
@property (nonatomic,strong) UIImageView *bgImageView2;
@property(nonatomic,assign) CGFloat imageH;



@property (weak, nonatomic) IBOutlet UIButton *registerButton;
@property (weak, nonatomic) IBOutlet UIButton *loginButton;

@property(nonatomic,assign) BOOL amination;


@end

@implementation MTLoginFristController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIImage *image = [UIImage imageNamed:@"tour_scroll_bg_image"];
    CGFloat imageWidth = image.size.width;
    _imageH = imageWidth * HEIGHT / WIDTH ;
    _bgImageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, _imageH)];
    _bgImageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, _imageH, WIDTH, _imageH)];
    [self.view insertSubview:_bgImageView1 atIndex:0];
    [self.view insertSubview:_bgImageView2 atIndex:0];
    _bgImageView1.image = image;
    _bgImageView2.image = image;
    [self.loginButton setTitle:NSLTLocalizedString(@"login.login", nil) forState:UIControlStateNormal];
    [self.registerButton  setTitle:NSLTLocalizedString(@"register.nowregister", nil) forState:UIControlStateNormal];
    [self startAmiation1];
    
    
    UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
    //必须给effcetView的frame赋值,因为UIVisualEffectView是一个加到UIIamgeView上的子视图.
    effectView.frame = self.loginButton.bounds;
    [self.loginButton addSubview:effectView];
    self.loginButton.backgroundColor = [UIColor clearColor];
    UITapGestureRecognizer *gester = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(loginGesterClick:)];
    [effectView addGestureRecognizer:gester];
    [self updateApp];
}


-(void)updateApp
{
    
    [[LTThridKeyCfgManger manager] getVersionInfoWithCompletion:^(UpdateModel *model, MTError *error) {
        if(error == nil)
        {
            if (model.hasNewVersion)
            {
                UIAlertController *alterVc = [UIAlertController alertControllerWithTitle:NSLTLocalizedString(@"update.versionUpate", nil) message:model.message preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *action = [UIAlertAction actionWithTitle:NSLTLocalizedString(@"update.update", ) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    NSURL *url = [NSURL URLWithString:model.downloadUrl];
                    [[UIApplication sharedApplication] openURL:url];
                }];
                UIAlertAction *cancleaction = [UIAlertAction actionWithTitle:NSLTLocalizedString(@"update.noupdate", ) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                }];
                [alterVc addAction:action];
                if (!model.forceUpdate)
                {
                    [alterVc addAction:cancleaction];
                }
                [self presentViewController:alterVc animated:YES completion:nil];
            }
        }
    }];
    
}


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    _amination = YES;
    [self startAmiation1];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
    _amination = NO;
}


-(void)startAmiation1
{
    if(_amination)
    {
        self.bgImageView1.y = 0;
        self.bgImageView2.y = _imageH;
        [UIView animateWithDuration:20 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
            self.bgImageView1.y = - self.imageH;
            self.bgImageView2.y = 0;
        } completion:^(BOOL finished) {
            self.bgImageView2.y =  0;
            self.bgImageView1.y = self.imageH;
            if(finished)
            {
             [self startAmiation2];
            }
        }];
    }
    
}

-(void)startAmiation2
{
    if(_amination)
    {
    self.bgImageView2.y =  0;
    self.bgImageView1.y = self.imageH;
    [UIView animateWithDuration:20 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        self.bgImageView2.y = - self.imageH;
        self.bgImageView1.y = 0;
    } completion:^(BOOL finished) {
        self.bgImageView1.y =  0;
        self.bgImageView2.y = self.imageH;
        if(finished)
        {
            
            [self startAmiation1];
        }
    }];
    }
}

-(void)stopAmation{
    [self.bgImageView1.layer removeAllAnimations];
    [self.bgImageView2.layer removeAllAnimations];
    [self.view.layer removeAllAnimations];

//    [UIView]
}


- (IBAction)registerClick:(UIButton *)sender {
    [self stopAmation];
    MTRegisterViewController *registerVc = [MTRegisterViewController baseStoryBordWithName:@"Login"];
    [self.navigationController pushViewController:registerVc animated:YES];
}
- (IBAction)loginClick:(UIButton *)sender {
    [self stopAmation];
    MTLoginViewController *loginVc = [MTLoginViewController baseStoryBordWithName:@"Login"];
    [self.navigationController pushViewController:loginVc animated:YES];
    
}
-(void)loginGesterClick:(UITapGestureRecognizer *)sender
{
    [self stopAmation];
    MTLoginViewController *loginVc = [MTLoginViewController baseStoryBordWithName:@"Login"];
    [self.navigationController pushViewController:loginVc animated:YES];
}


@end
